package com.glorysoft.knowlespda.ui.setting

import android.app.Activity
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import androidx.databinding.ObservableArrayList
import androidx.lifecycle.MutableLiveData
import com.glorysoft.knowlespda.BR
import com.glorysoft.knowlespda.R
import com.glorysoft.knowlespda.base.BaseViewModel
import com.glorysoft.knowlespda.base.Constant
import com.glorysoft.knowlespda.entity.PairingEntity
import com.glorysoft.knowlespda.entity.ui.ToastMessageBean
import com.glorysoft.knowlespda.model.PairRepository
import com.google.gson.JsonObject
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * FileName: SettingViewModel
 * Author: XJH
 * Date: 2022/1/11 18:06
 * Description:
 * History:
 */
class SettingViewModel : BaseViewModel() {
    var constant = MutableLiveData<Constant.Companion>()
    val pairRepository by lazy { PairRepository() }
    var pdaNo : String = ""

    var pairItems = ObservableArrayList<PairingEntity>()
    var pairBinding =
        ItemBinding.of<PairingEntity>(BR.pairingItem, R.layout.item_pairing)

    fun initVersion(context: Activity){
        constant.value = Constant.Companion
        val manager: PackageManager = context.packageManager
        var info: PackageInfo? = null
        try {
            info = manager.getPackageInfo(context.packageName, 0)
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        val version = info!!.versionName
        constant.value?.VRESION = version
    }

    fun getAuthority(){
        launchOnlyResult({
            val authority = pairRepository.getAuthority(JsonObject())
            pairItems.clear()
            pdaNo = authority.pdano
            if (authority.pairmachines.isNotEmpty()){
                val split = authority.pairmachines.split(";")
                split.forEach {
                    val data = PairingEntity()
                    data.pairmachines = it
                    pairItems.add(data)
                }
            }
            defUI.callObserve.call()
        })
    }

    fun unPair(){
        val str = StringBuffer()
        pairItems.forEach {
            if (it.isSelector){
                str.append(it.pairmachines + ";")
            }
        }

        if (str.toString().isEmpty()){
            defUI.toastEvent.value = ToastMessageBean("Unchecked data",false)
            return
        }

        val  map = JsonObject()
        map.addProperty("OPID",Constant.CURRENT_USER)
        map.addProperty("UNPAIRMACHINES",str.toString().substring(0,str.toString().length-1))
        launchOnlyResult({
            pairRepository.unPair(map)
//            val iterator = pairItems.iterator()
//            while (iterator.hasNext()){
//                if (iterator.next().isSelector){
//                    iterator.remove()
//                }
//            }
//            defUI.callObserve.call()
            getAuthority()
        })
    }

    fun addPair(s : String){
        if (s.isEmpty()){return}
        val  map = JsonObject()
        map.addProperty("OPID",Constant.CURRENT_USER)
        if (s.endsWith(";")){
            map.addProperty("PAIRMACHINES",s.substring(0,s.length-1))
        }else{
            map.addProperty("PAIRMACHINES",s)
        }
        launchOnlyResult({
            pairRepository.pairMachine(map)
            getAuthority()
        })
    }


}